<?php

// +----------------------------------------------------------------------
// | 版权所有 2016~2018 路人甲乙科技有限公司 [ http://www.lurenjiayi.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://lurenjiayi.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | @author 张继立 <404851763@qq.com>
// +----------------------------------------------------------------------
namespace app\member\home;

use app\index\controller\Home;
use think\Db;
class Index extends Home
{
    //构造函数-用户验证
    public function __construct(){
        parent::__construct();
        
        if(!is_numeric(ID) || ID<1){
            if(isMobile()){
                return $this->redirect('/wap/index');
            }else{
                return $this->redirect('/login.html');
            }
        }
        
        $bdbanknum=db('member_banks')->where('uid',ID)->count();    //得到绑定银行数据，用于显示安全等级
        $this->assign('bdbanknum',$bdbanknum);
    }
    
    /**
     * 用户中心
     * @return mixed
     */
    public function index()
    {
        $time = strtotime(date("Y-m-d", time()));
        $qd_result = db("member_qd")->whereTime('add_time','between',[$time,($time + 3600 * 24)])
                    ->where('uid',ID)->count();
        $this->assign('issign', $qd_result);            //发送是否签到
        
        
        $stockinfo=getallStocks(['uid'=>ID,'status'=>'2']);
        $this->assign('stocknum',$stockinfo['pagerows']);
        if($stockinfo['pagerows']>0){
            foreach ($stockinfo['data'] as $key=>&$hang){
                $stockinfo['data'][$key]['totalinterest']=0;
                $stockinfo['data'][$key]['dkmoney']=0;
                
                if(in_array($hang['type'], ['1','2','21','22'])){
                    $totalinterest=$hang['borrow_interest'];
                    $totaldikou=$hang['dikou_interest'];
                    if($hang['stock_addfinancing']>0){
                        $resu3 = Db::name('stock_addfinancing')->where(['borrow_id'=>$hang['id'],'uid'=>$hang['uid'],'status'=>'1'])->field('sum(borrow_interest) sum_borrow_interest,sum(dikou_interest) sum_dikou_interest')->find();
                        $totalinterest+=(isset($resu3['sum_borrow_interest']) && $resu3['sum_borrow_interest']>0)?$resu3['sum_borrow_interest']:0;
                        $totaldikou+=(isset($resu3['sum_dikou_interest']) && $resu3['sum_dikou_interest']>0)?$resu3['sum_dikou_interest']:0;
                    }
                    
                    $resu4 = Db::name('stock_renewal')->where(['borrow_id'=>$hang['id'],'uid'=>$hang['uid'],'status'=>'1'])->field('sum(borrow_fee) sum_borrow_interest,sum(dikou_interest) sum_dikou_interest')->find();
                    $totalinterest+=(isset($resu4['sum_borrow_interest']) && $resu4['sum_borrow_interest']>0)?$resu4['sum_borrow_interest']:0;
                    $totaldikou+=(isset($resu4['sum_dikou_interest']) && $resu4['sum_dikou_interest']>0)?$resu4['sum_dikou_interest']:0;
                    
                    $stockinfo['data'][$key]['totalinterest']=$totalinterest;
                    $stockinfo['data'][$key]['dkmoney']=$totaldikou;
                }
            }
        }
        $this->assign('stockinfo',$stockinfo['data']);
    
        $stockglobal=get_stock_setting();
        $is_tiying=(isset($stockglobal['is_tiying']) && $stockglobal['is_tiying']=='0')?'0':'1';
        $this->assign('is_tiying',$is_tiying);
        
        $this->assign('parse_url','index');
        $this->assign('munv','index');
        
        return $this->fetch();
    }
    

    //空返回
    public function _empty(){
        if(isMobile()){
            return $this->redirect('/wap/index');
        }else{
            return $this->redirect('/index');
        }
    }
}
